package csu.web.mypetstore.web.servlet;

import csu.web.mypetstore.domain.Account;
import csu.web.mypetstore.service.AccountService;
import csu.web.mypetstore.service.LogService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class NewAccountServlet extends HttpServlet {
    private static final String SIGN_ON_FORM = "/WEB-INF/jsp/account/signon.jsp";
    private static final String REGISTER_FORM = "/WEB-INF/jsp/account/register.jsp";

    private Account account= new Account();

    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req,resp);
    }

    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String userid= req.getParameter("userid");
        String password = req.getParameter("password");
        String repeatedPassword = req.getParameter("repeatedPassword");
        String vCode = req.getParameter("vCode");
        String firstName = req.getParameter("firstName");
        String lastName = req.getParameter("lastName");
        String email = req.getParameter("email");
        String phone = req.getParameter("phone");
        String address1 = req.getParameter("address1");
        String address2 = req.getParameter("address2");
        String city = req.getParameter("city");
        String state = req.getParameter("state");
        String zip = req.getParameter("zip");
        String country = req.getParameter("country");
        String languages = req.getParameter("languagePreference");
        String categories = req.getParameter("favouriteCategoryId");

        boolean listOption;
        if(req.getParameter("listOption") != null){
            listOption =  true;
        }else{
            listOption = false;
        }

        boolean bannerOption;
        if(req.getParameter("bannerOption") != null){
            bannerOption =  true;
        }else{
            bannerOption = false;
        }

        if(password.equals(repeatedPassword)){
            HttpSession session = req.getSession();

            if(session.getAttribute("code").equals(vCode)) {
                account = new Account();
                account.setUserid(userid);
                account.setUsername(username);
                account.setPassword(password);
                account.setFirstName(firstName);
                account.setLastName(lastName);
                account.setEmail(email);
                account.setPhone(phone);
                account.setAddress1(address1);
                account.setAddress2(address2);
                account.setCity(city);
                account.setState(state);
                account.setZip(zip);
                account.setCountry(country);
                account.setLanguagePreference(languages);
                account.setFavouriteCategoryId(categories);
                account.setListOption(listOption);
                account.setBannerOption(bannerOption);

                AccountService accountService = new AccountService();
                accountService.insertAccount(account);

                Account account = (Account) session.getAttribute("account");
                if(account != null) {
                    String strUrl = "http://" + req.getServerName() + ":" + req.getServerPort()
                            + req.getContextPath() + req.getServletPath() + "?" + req.getQueryString();
                    LogService logService = new LogService();
                    String logInfo = logService.logInfo(" ") + "用户" +account.getUsername()+ "  "+"注册成功转至登陆页面";
                    logService.insertLogInfo(account.getUsername(),logInfo);
                }

                req.getRequestDispatcher(SIGN_ON_FORM).forward(req,resp);
            } else {
                req.getRequestDispatcher(REGISTER_FORM).forward(req,resp);
            }

        } else {
            req.getRequestDispatcher(REGISTER_FORM).forward(req,resp);
        }

    }
}
